package com.example.service.implService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.ResponseVO;
import com.example.mapper.HomeworkMapper;
import com.example.pojo.Homework;
import com.example.pojo.dto.HomeworkDTO;
import com.example.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author GovQ
 * @description 针对表【homework】的数据库操作Service实现
 * @createDate 2024-06-18 22:13:35
 */
@Service
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework>
        implements HomeworkService {

    @Resource
    private HomeworkMapper homeworkMapper;

    @Autowired
    private ScheduledExecutorService scheduledExecutorService;

    @Override
    public ResponseEntity<ResponseVO<String>> addOrUpdateHomeWork(Homework homework, String releaseDateTime) {
        if (Objects.isNull(homework.getHomeworkId())) {
            homeworkMapper.insert(homework);
            // 先添加进去 然后判断是否是定时任务
            if (homework.getIsRelease() == 2) {
                scheduleHomeworkRelease(homework, releaseDateTime);
            }
        } else {
            // 做修改操作
            System.out.println("做修改操作");
            homeworkMapper.updateById(homework);
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "操作成功"));
    }

    @Override
    public ResponseEntity<ResponseVO<List<HomeworkDTO>>> getAllHomeworkByClassId(Long classId) {
        List<HomeworkDTO> pushClassId = homeworkMapper.getAllHomeworkByClassId(classId);
        if (!pushClassId.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", pushClassId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "该班级还没有作业"));
    }

    @Override
    public ResponseEntity<ResponseVO<List<HomeworkDTO>>> getHomeworkByTeacherId(Long teacherId) {
        List<HomeworkDTO> pushClassId = homeworkMapper.getHomeworkByTeacherId(teacherId);
        if (!pushClassId.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", pushClassId));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "该班级还没有作业"));
    }

    // 根据作业编号获取数据
    @Override
    public ResponseEntity<ResponseVO<HomeworkDTO>> getHomeworkDetail(Long homeworkId) {
        HomeworkDTO homework = homeworkMapper.getHomeworkDetail(homeworkId);
        if (Objects.isNull(homework)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "未查询到该作业编号的数据"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "查询成功", homework));
    }


    private void scheduleHomeworkRelease(Homework homework, String releaseDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime releaseTime = LocalDateTime.parse(releaseDateTime, formatter);

        long delay = ChronoUnit.MILLIS.between(LocalDateTime.now(), releaseTime);

        scheduledExecutorService.schedule(() -> {
            // 执行发布作业的逻辑
            releaseHomework(homework);
        }, delay, TimeUnit.MILLISECONDS);
    }

    private void releaseHomework(Homework homework) {
        // 这里是实际发布作业的逻辑，例如更新数据库中的发布状态等
        homework.setIsRelease(1);
        homeworkMapper.updateById(homework);
    }
}




